package classes.view.component 
{
	import classes.controller.DAOSendFlash;
	import classes.controller.EDataObject;
	import classes.controller.VONullObject;
	import classes.model.VOUserInfo;
	
	import com.d4p.custom.customScrollBar.Scrollbar;
	import com.d4p.custom.defaultButton.DefaultBtn;
	import com.d4p.custom.imageLoader.ImageLoader;
	import com.d4p.custom.imageLoader.ImageLoaderEvent;
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Cubic;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class PopupPersonal extends MovieClip
	{
		private var _name:TextField;
		private var _add:DefaultBtn;
		private var _singo:DefaultBtn;
		
		private var _aa1:MovieClip;
		private var _a1:MovieClip;
		private var _a2:MovieClip;
		private var _a3:MovieClip;
		private var _b1:MovieClip;
		private var _level:MovieClip;
		private var _add_alt_mc:MovieClip;
		
		private var _id:TextField;
		private var _win:TextField;
		private var _lose:TextField;
		private var _per:TextField;
		private var _score:TextField;
		private var _num:TextField;
		private var _score2:TextField;
		private var _num2:TextField;
		private var _imgCont:Sprite;
		
		private var _close:DefaultBtn;
		
		private var _friendBool:Boolean;
		
		public function PopupPersonal() 
		{
			super();
			setLayOut();
			init();
			defaultSetting();
		}
		
		private function setLayOut():void
		{
			this._name			= this.getChildByName('name_txt')		as TextField;
			this._add			= this.getChildByName('add_mc')			as DefaultBtn;
			this._add_alt_mc	= this.getChildByName('add_alt_mc')		as MovieClip;
			this._singo			= this.getChildByName('singo_mc')		as DefaultBtn;
			this._aa1			= this.getChildByName('aa1_mc')			as MovieClip;
			this._a1			= this.getChildByName('a1_mc')			as MovieClip;
			this._a2			= this.getChildByName('a2_mc')			as MovieClip;
			this._a3			= this.getChildByName('a3_mc')			as MovieClip;
			this._b1			= this.getChildByName('b1_mc')			as MovieClip;
			this._level			= this.getChildByName('level_mc')		as MovieClip;
			this._id			= this.getChildByName('id_txt')			as TextField;
			this._win			= this.getChildByName('win_txt')		as TextField;
			this._lose			= this.getChildByName('lose_txt')		as TextField;
			this._per			= this.getChildByName('per_txt')		as TextField;
			this._score			= this.getChildByName('score_txt')		as TextField;
			this._num			= this.getChildByName('num_txt')		as TextField;
			this._score2		= this.getChildByName('score2_txt')		as TextField;
			this._num2			= this.getChildByName('num2_txt')		as TextField;
			this._imgCont		= this.getChildByName('img_mc')			as Sprite;
			this._close			= this.getChildByName('close_mc')		as DefaultBtn; 
		}
		
		private function defaultSetting():void
		{
			TweenMax.to(_add_alt_mc, 0, { autoAlpha:0 } ); // 친구추가되었습니다 ALT임..
			
			var textformat_letterspacing:TextFormat = new TextFormat();
			textformat_letterspacing.letterSpacing = -1;
			
			_name.setTextFormat(textformat_letterspacing);
			
			_add_alt_mc.buttonMode = true;
			
		}
		private function init():void
		{
			
			_add.addEventListener(MouseEvent.CLICK, 	addClick);
			_singo.addEventListener(MouseEvent.CLICK, 	singoClick);
			_close.addEventListener(MouseEvent.CLICK, 	closeClick);
			_add_alt_mc.addEventListener(MouseEvent.CLICK,	onClickAlt);
		}
		
		private function addClick(e:MouseEvent):void 
		{
			_friendBool =! _friendBool;
			if(_friendBool){
				trace("친구추가");
				_add_alt_mc.y = 484;
				TweenMax.to(_add_alt_mc, 0.4, {y:498, autoAlpha:1, ease:Cubic.easeOut } );
				
				
				var obj:Object = {
					"data":[
						{
							"fr_id":_id.text
							,"mb_id":VOUserInfo.getDataInstance().data.id
						}
					]
				}
				
				DAOSendFlash.getLoadInstance().setData('friendAdd', obj);
				
				
				
				
			}else{
				TweenMax.to(_add_alt_mc, 0.3, { y:488, autoAlpha:0, ease:Cubic.easeIn } );
			}
			
		}
		
		private function singoClick(e:MouseEvent):void 
		{
			
			
			
			trace("신고하기");
			var obj:Object = {};
			DAOSendFlash.getLoadInstance().setData('friendAlert', obj);
			
			
			
			
		}
		
		private function onClickAlt(e:MouseEvent):void
		{
			_friendBool = false;
			TweenMax.to(_add_alt_mc, 0.3, { y:488, autoAlpha:0, ease:Cubic.easeIn } );
		}
		private function closeClick(e:MouseEvent):void
		{
			_friendBool = false;
			TweenMax.to(_add_alt_mc, 0, { autoAlpha:0 } );
			this.dispatchEvent(new EDataObject(["close"], EDataObject.EVENT_DATAOBJECT));
		}
		
		public function data_init($obj:Object):void
		{
			loadUserInfo = $obj;
		}
		
		private function set loadUserInfo($data:Object):void 
		{
			if(($data.profileimage == null) || ($data.profileimage == "undefined")){
				this.profileimageLoad	= "";
			}else{
				this.profileimageLoad	= $data.profileimage;
			}
			
			_name.text 			= VONullObject.getLoadInstance().nullText($data.nick);
			_id.text 			= VONullObject.getLoadInstance().nullText($data.mb_id);
			_win.text 			= VONullObject.getLoadInstance().nullText($data.win);
			_lose.text 			= VONullObject.getLoadInstance().nullText($data.lose);
			_per.text 			= Number(Number($data.win) / (Number($data.win) + Number($data.lose)) * 100).toFixed(0);
			
			_aa1.x 				= _name.x + _name.textWidth + 3;
			_id.x 				= _aa1.x + _aa1.width + 2;
			
			_a1.x 				= _win.x + _win.textWidth + 3;
			_lose.x 			= _a1.x + _a1.width + 5;
			_a2.x 				= _lose.x + _lose.textWidth + 3;
			_per.x 				= _a2.x + _a2.width + 2;
			_a3.x 				= _per.x + _per.textWidth - 4;
			
			_score.text 		= VONullObject.getLoadInstance().nullText($data.nowScore);
			_num.text 			= VONullObject.getLoadInstance().nullText($data.nowRank);
			_score2.text 		= VONullObject.getLoadInstance().nullText($data.totalScore);
			_num2.text 			= VONullObject.getLoadInstance().nullText($data.totalRank);
			
			_b1.x 				= _score2.x + _score2.textWidth + 3;
			
			var a:Number 		= Number($data.level);
			_level.text_txt.text= String($data.level);
			
			var levelNum:int 	= Math.floor(Number($data.level)/10)+1;
			_level.bg_mc.gotoAndStop(levelNum);
		}
		
		private function set profileimageLoad($url:String):void
		{
			if($url!=""){
				//imageload.
				var imgLoader:ImageLoader = new ImageLoader();
				imgLoader.load($url, false);
				imgLoader.addEventListener(ImageLoaderEvent.IMAGE_COMPLETE,	onCompleteImageLoad);	
			}
		}
		private function onCompleteImageLoad(e:ImageLoaderEvent):void
		{
			this._imgCont.addChild(e.bitmap);
			this._imgCont.alpha = 0;
			TweenLite.to(_imgCont, 0.3, {alpha:1});
		}
		
	}
	
}